Preview - Intro To Netlogo Programming For Educators

Procedures


This page will introduce you to procedures, one of the most important ideas in NetLogo and programming in general. Follow along with the steps below.

 


Questions

Please answer the questions below.

Creating Procedures

If you had to type every command each time you wanted it to run, it would take forever to run computer programs. Instead, we can put a bunch of commands together into a procedure. Open up the NetLogo Code tab and we'll go through the steps of writing a procedure.

  • You start a procedure by typing the keyword to and then the name of the procedure. You end the procedure with the keyword endLet's make a procedure called move-turtles. First write to move-turtles. Then on the next few lines, ask the turtles to move forward 0.175 and turn right 1. (use these exact numbers for now to prepare for a later question). Then on a new line write end.
  • If you are using NetLogo Web, click the recompile code button at the top of the NetLogo Code tab when you finish writing the procedure; if you are using NetLogo Desktop you don't have to do anything.
  • Now open the command center back up and type move-turtles. Each time you call move, all the commands inside the move procedure will get run.

Copy and paste your move procedure below. 


Buttons

To make things even easier, we can create a button to call a procedure. 

  • If you are using NetLogo Web, first click the lock button that looks like this:  so that you enter authoring mode. Then you can right click on the green area and click Create Button. A window for creating a button will appear. In the commands box type move-turtles and press okay.
  • If you are using NetLogo Desktop, you can just right click on the white area next to the model "view" and click Button. A window for creating a button will appear. In the commands box type move-turtle and press okay. 

Now you can click the button to call the procedure. 

 


The Setup Procedure

As a convention, almost every NetLogo model has a procedure called setup with an accompanying button. This procedure is to set up the "world" of the model.

  • Write a procedure called setup. For now, just clear everything and then create some turtles. Ask them to move forward 10 and turn right 90. Use these exact numbers for now to prepare for the next questions. (If you are using NetLogo web, don't forget to recompile code afterwards)
  • Create a button for you setup procedure.

Each time you click the setup button, all the existing turtles should be cleared and new ones created.

Copy and paste your setup procedure below.


The Go Procedure

As a convention, almost every NetLogo model also has a go procedure and an accompanying button. This is generally the procedure used to make a model run continuously. It makes sense to have a unit of time in a model when it runs continuously. In NetLogo this unit is called a tick.

  • To reset the clock in our model add the command reset-ticks  to your setup procedure.
  • Create a procedure called go. For now just call your move-turtles procedure and the tick command. 
  • Create a button for your go procedure. In the window for creating the button, check the box that says "forever" and also check the box that says "disable until ticks start". (If you closed the box, you can right click on the button and click "edit" to open it again).

When you click the go button, it should now call the go procedure repeatedly. Each time the go procedure is executed, the ticks increase by 1, because you called the tick command inside the go procedure. 

What behavior do you see? Were you surprised by it? Why is this behavior occurring? Answer in the box below.  


Predicting effects of small changes

What do you think will happen if you change the code of your move-turtles procedure so that the turtles move forward a little bit more each tick? Write your prediction below. 

 


Integrative Understanding

Change your move-turtles procedure so that turtles move forward slightly more each tick (something like 0.22). What pattern emerges? (You can use the Model Speed slider at the top of the model to make the model run faster or slower).

Most people do not predict the resulting pattern. Some predict that the turtles will move in a smaller or larger pattern, but rarely the actual emergent pattern of a pulsating circle. This illustrates one of the difficulties in understanding emergent systems. It is difficult to predict how a small change enacted by many agents will "integrate" to form a larger pattern. This is sometimes called "integrative understanding," as it parallels the cumulative integration of small differences in calculus.

Can you think of a real world system in which integrative understanding is difficult?


Changing Colors and Drawing Paths

You can change turtles' color with  ask turtles [set color color]where color can be any of: grey, red, orange, brown, yellow, green, turquoise, cyan, sky, blue, violet, magenta and pink. There are other ways of representing colors with more flexibility described here in the NetLogo Programming Guide

  • Using the command center, ask some turtles to set their color.

 

Try asking the turtles to pen-down. What happens? Try pen-erase and pen-up as well. If you want you can read about these commands here.

  • Try asking the turtles to pen-down and then run your model. What do you see? Does this change your understanding of the emergent pulsing-circle pattern?

If you are using NetLogo Web, copy your code here, because when you leave this page the code you entered in the NetLogo Code tab won't be saved. If you are using NetLogo Desktop, still copy your code here, but you can also save your model on your computer if you want to.


If you had any issues on this page, describe them below. If not, type something like "no problems" and continue on.


Notes

These notes will appear on every page in this lesson so feel free to put anything here you'd like to keep track of.